package com.tt.lcd.qunyangGPRS.dao.renshen;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.tt.lcd.qunyangGPRS.dao.Mysql;
import com.tt.lcd.qunyangGPRS.entity.XiaoQunYnag;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class XiaoqunyangDao {
	private Mysql mysql;

	public static String createSQL = "CREATE TABLE IF NOT EXISTS xiaoqunyang (id integer primary key autoincrement,"
			+ "UID varchar(35),"
			+ "time varchar(12),"
			+ "log BLOB,"
			+ "logsendtime varchar(12),"// 暂时没用
			+ "nowLog BLOB,"
			+ "nowLogsendTime varchar(12),"
			+ "nowLogrecTime varchar(12)," + "nowSendcout integer" + ")";
	public XiaoqunyangDao(Context context){
		mysql = new Mysql(context);
	}
	/***
	 * 创建
	 */
	public void create() {
		Log.e("test",mysql.toString());
		SQLiteDatabase sd = mysql.getWritableDatabase();
		sd.execSQL(createSQL);
		sd.close();
	}

	/***
	 * 插入
	 *
	 * @return
	 */
	public String insert(String Uid) {
		String messge = "插入失败！";
		SQLiteDatabase sd = mysql.getWritableDatabase();

		ContentValues cv = new ContentValues();
		cv.put("UID", Uid);
		byte[] b = new byte[0];
		cv.put("log", b);
		cv.put("time", "1990-1-2");
		cv.put("logsendtime", "1990-1-1");
		byte[] b1 = new byte[0];
		cv.put("nowLog", b1);
		cv.put("nowLogsendTime", "1990-1-1 00:00:00");
		cv.put("nowLogrecTime", "1990-1-1 00:01:00");
		cv.put("nowSendcout", 0);
		sd.insert("xiaoqunyang", null, cv);
		sd.close();

		messge = "插入成功！";
		return messge;
	}

	/***
	 * 查询返回所有
	 *
	 * @return
	 */
	public ArrayList<XiaoQunYnag> selectAll() {
		ArrayList<XiaoQunYnag> list = new ArrayList<XiaoQunYnag>();
		SQLiteDatabase sd = mysql.getWritableDatabase();

		// Cursor cur=sd.query("xiaoqunyang", new String[]{"UID"}, null, null,
		// null, null, null);
		Cursor cur = sd.rawQuery("select * from xiaoqunyang", null);

		while (cur.moveToNext()) {
			XiaoQunYnag x = new XiaoQunYnag();
			x.setId(cur.getInt(0));
			x.setUid(cur.getString(1));
			x.setTime(cur.getString(2));
			x.setData(cur.getBlob(3));
			x.setLogSendtime(cur.getString(4));
			x.setNowLog(cur.getBlob(5));
			x.setNowLogSendtime(cur.getString(6));
			x.setNowLogRedtime(cur.getString(7));
			x.setNowSendcout(cur.getInt(8));
			list.add(x);
		}

		cur.close();
		sd.close();

		return list;
	}

	/***
	 * 查询返回所有站的历史记录数组
	 *
	 * @return
	 */
	public ArrayList<byte[]> select() {
		ArrayList<byte[]> list = new ArrayList<byte[]>();
		SQLiteDatabase sd = mysql.getWritableDatabase();

		Cursor cur = sd.query("xiaoqunyang", new String[] { "log" }, null,
				null, null, null, null);

		while (cur.moveToNext()) {
			// 将Blob数据转化为字节数组
			byte[] b = null;
			b = cur.getBlob(cur.getColumnIndex("log"));
			list.add(b);
		}

		cur.close();
		sd.close();

		return list;
	}

	/***
	 * 修改
	 *
	 * @return
	 */
	public String updata(byte[] b, String uid) {
		String messge = "修改失败！";
		SQLiteDatabase sd = mysql.getWritableDatabase();

		ContentValues values = new ContentValues();// 是用map封装的对象，用来存放值
		values.put("log", b);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		values.put("time", sdf.format(new Date()));
		// table:表名, values：更新的值, whereClause:更新的条件,
		// whereArgs：更新条件的占位符的值,返回值：成功修改多少行
		int result = sd.update("xiaoqunyang", values, "UID = ?",
				new String[] { uid });
		// 关闭数据库对象
		sd.close();

		messge = "修改成功！";
		return messge;
	}

	/**
	 * 根据UID删除
	 *
	 * @param UID
	 */
	public String deleteForUID(String UID) {
		String messge = "删除失败！";
		SQLiteDatabase sd = mysql.getWritableDatabase();

		sd.execSQL("delete from xiaoqunyang where UID='" + UID + "'");
		// 关闭数据库对象
		sd.close();

		messge = "删除成功！";
		return messge;
	}

	/**
	 * 判断新设备是否已经存在
	 *
	 * @param UID
	 */
	public boolean panduanUID(String UID) {
		boolean bl = false;
		SQLiteDatabase sd = mysql.getWritableDatabase();
		Cursor cur = sd.rawQuery("select * from xiaoqunyang where UID='" + UID
				+ "'", null);
		while (cur.moveToNext()) {
			// 将Blob数据转化为字节数组
			bl = true;
		}
		cur.close();
		sd.close();
		return bl;
	}

	/****
	 * 修改当前量查询时间及查询次数
	 */
	public String updataNowSendTime(String uid,String time){
		String messge = "修改失败！";
		SQLiteDatabase sd = mysql.getWritableDatabase();
		Cursor cur = sd.query("xiaoqunyang", new String[] { "nowSendcout" }, null,
				null, null, null, null);

		int b = 0;
		while (cur.moveToNext()) {
			// 将Blob数据转化为字节数组

			b = cur.getInt(cur.getColumnIndex("nowSendcout"));
		}

		cur.close();

		ContentValues values = new ContentValues();// 是用map封装的对象，用来存放值
		values.put("nowLogsendTime",time);
		values.put("nowSendcout", b+1);
		// table:表名, values：更新的值, whereClause:更新的条件,
		// whereArgs：更新条件的占位符的值,返回值：成功修改多少行
		int result = sd.update("xiaoqunyang", values, "UID = ?",
				new String[] { uid });
		// 关闭数据库对象
		sd.close();

		messge = "修改成功！";
		return messge;
	}

	/***
	 * 刷新当前量数据
	 *
	 * @return
	 */
	public String updataNowLog(byte[] b, String uid) {
		String messge = "修改失败！";
		SQLiteDatabase sd = mysql.getWritableDatabase();

		ContentValues values = new ContentValues();// 是用map封装的对象，用来存放值
		values.put("nowLog", b);
		values.put("nowSendcout", 0);// 未收到数据的次数归0
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		values.put("nowLogrecTime", sdf.format(new Date()));
		// table:表名, values：更新的值, whereClause:更新的条件,
		// whereArgs：更新条件的占位符的值,返回值：成功修改多少行
		int result = sd.update("xiaoqunyang", values, "UID = ?",
				new String[] { uid });
		// 关闭数据库对象
		sd.close();

		messge = "修改成功！";
		return messge;
	}

	/****
	 * 修改历史记录查询时间
	 */
	public String updataLogSendTime(String uid,String time){
		String messge = "修改失败！";
		SQLiteDatabase sd = mysql.getWritableDatabase();


		ContentValues values = new ContentValues();// 是用map封装的对象，用来存放值
		values.put("logsendtime",time);
		// table:表名, values：更新的值, whereClause:更新的条件,
		// whereArgs：更新条件的占位符的值,返回值：成功修改多少行
		int result = sd.update("xiaoqunyang", values, "UID = ?",
				new String[] { uid });
		// 关闭数据库对象
		sd.close();

		messge = "修改成功！";
		return messge;
	}
}
